And this video I'm going to show you how to shift all dates of a specific date time index for example
how to add one or more days weeks months or even years to each and every timestamps of the day time
index.
So first of all we import pandas and then let's assume that we have a data frame with a daytime index.
So for example the S&P 500 data frame with historical prices for the S&P 500 index.
But actually we do not care about the prices so we could also have a data frame with orders for a product.
And let's assume latest two calendar days after each and every order.
We have to ship the product and therefore we need to get the shipment data by adding two days to the
order date.
So let's import here.
The data frame and here we have the daytime index starting in nineteen hundred seventy on the thirty
first of December and actually to add two days to each and every time stamp here.
It would be quite intuitive to simply coat here S&P 500 index plus 2.
But of course this doesn't work.
Actually here but there is a solution for it.
And we can actually do this with the direct pen us method that date offset and let's have a look here
inside and here we can define actually a standard kind of date increment used for a date range and TV
and actually define our date increment and uh the default setting is actually one day.
But we can also pass a two to the dates parameter.
So let's run the sale here and if we get a date offset object.
For the time increment of two days.
So again this is a pen does the date offset object.
And first of all let's again get the dates of our original day time index and now we want to shift each
and every time stamp here by two days.
Then we can actually add to our index the date offset object
so we have successfully added Our two days to the thirty first of December.
So here we have the second of January.
Then we shifted to the fourth of January to the sixth of January and so on and actually date offset
method provides so many more options here so we can also determine that we want to have increments of
one week.
So we add to one week.
So for example we shift at the thirty first of December nineteen hundred seventy two the seventh of
January in nineteen hundred seventy one and so on.
And of course we could also pass here two or three add to the week's parameter.
So for example also three weeks and of course you can also have increments of one months or two months
or three months.
So this is no problem here and here we get actually the thirty first of March instead of the thirty
first of December and it also works with a yes.
So we can shift each and every time stamp by ten years.
And instead of having here the thirty first of December nineteen hundred seventy we have the thirty
first of December nineteen hundred eighty and so on and finally we can also combine those increments.
So let's assume that you want to shift to each and every time stamp by one year to month.
Fifteen days the twelve hours and thirty minutes.
Then we can do this here
so here we have the 15th of March nineteen hundred seventy two at twelve thirty.
And so far we added a specific period to each and every time stamp.
But we can also say abstract to a period and go back to the past.
So for example we can go back one day by having a minus sign instead of a plus sign.
So this also works and instead of having you know the thirty first of December nineteen hundred seventy
we have here the 30th of December so that was the first video on shifting dates and I hope to see you
also in the next one by.